package com.yc.navy.goods.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.navy.goods.bean.MemberInfo;
import com.yc.navy.goods.bean.ShopInfo;
import com.yc.navy.goods.bean.UpdateStatusInfo;
import com.yc.navy.goods.dto.ConstantInfoDTO;
import com.yc.navy.goods.dto.MemberInfoDTO;
import com.yc.navy.goods.dto.ProductDTODTO;
import com.yc.navy.goods.dto.ShopInfoDTO;
import com.yc.navy.goods.dto.ShopInfoDTOO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.product.feign.IProductFeignClient;
import com.yc.navy.goods.service.IShopInfoService;
import com.yc.navy.goods.util.ConstantInfo;
import com.yc.navy.goods.util.StringUtil;
import com.yc.navy.goods.vo.ResultVO;
import com.yc.navy.goods.vo.StreamVO;


@RestController
@RequestMapping("/shop")
public class ShopInfoController {
	@Autowired
	private IShopInfoService shopInfoSerivce;
	
	@Autowired
	private IProductFeignClient productFeignClient;
	
	private String getlogin(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfoDTO.LOGINCHECK);
		if(obj == null) {
			return null;
		}
		
		MemberInfo m = new MemberInfo();
		MemberInfoDTO member = (MemberInfoDTO) obj;
		BeanUtils.copyProperties(member, m);
		System.out.println("MemberInfo = " + m);
		return m.getMid() +"";
	}
	
	@RequestMapping("/findsid")
	public StreamVO findsid(HttpSession session) {
		List<ShopInfoDTO> list = shopInfoSerivce.findsid();
		if(list.isEmpty() || list.size() <= 0) {
			return new StreamVO(ResultEnum.NOTFOUND);
		}
		return new StreamVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/joinshop")
	public ResultVO joinshop(ShopInfo shop,HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		int result = shopInfoSerivce.joinshop(shop);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@SuppressWarnings("unused")
	@RequestMapping("/check")
	public ResultVO check(HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		ShopInfoDTO result = shopInfoSerivce.check(Integer.parseInt(mid));
		if(result == null) {
			return new ResultVO(ResultEnum.NOT_DATA);
		}
		ShopInfoDTOO shopInfo = new ShopInfoDTOO();
		BeanUtils.copyProperties(result, shopInfo);
		
		return new ResultVO(ResultEnum.SUCCESS,shopInfo);
	}
	
	@RequestMapping("/createshop")
	public ResultVO createshop(HttpSession session,ShopInfo shop,MultipartFile photo) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		String savepath = "";
		File file = null;
		
		if(photo != null && photo.getSize() > 0) {
			try {
				savepath = ConstantInfo.UPLOADPATH + "/" + System.currentTimeMillis() + "_" +  photo.getOriginalFilename();
				file = new File(ConstantInfo.basepath,savepath);
				photo.transferTo(file);
				shop.setSpic(savepath);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		shop.setMid(Integer.parseInt(mid));
		int result = shopInfoSerivce.createshop(shop);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@RequestMapping("/findList")
	public StreamVO findList(HttpSession session,@RequestParam Map<String,Object> map) {
		return shopInfoSerivce.findList(map);
	}
	
	@RequestMapping("/brandList")
	public ResultVO brandList(HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		List<ShopInfo> list = shopInfoSerivce.brandList();
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@RequestMapping("/shopOne")
	public ResultVO shopOne(HttpSession session,Integer sid) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		ShopInfo shop = shopInfoSerivce.shopOne(sid);
		if(shop == null ) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,shop);
	}
	
	@Transactional
	@RequestMapping("/findShopid")
	public ResultVO findShopid(HttpSession session,Integer pid) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		ProductDTODTO product = new ProductDTODTO();
		product.setPid(Long.valueOf(pid));
		Integer sid =  productFeignClient.checkshopid(product);
		
		ShopInfoDTOO shopinfo = shopInfoSerivce.ShopOne(sid);
		if(shopinfo == null ) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,shopinfo);
	}
	
	@RequestMapping("/updateStatus")
	public ResultVO updateStatus(HttpSession session,Integer sid,Integer sstatus) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		UpdateStatusInfo update = new UpdateStatusInfo();
		update.setSid(sid);
		update.setStatus(sstatus);
		System.out.println(update + "shop ==== updateStatus");
		Integer result = shopInfoSerivce.updatestatus(update);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@RequestMapping("/findShop")
	public ResultVO findShop(HttpSession session,Integer sid) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}
		
		ShopInfoDTOO shopinfo = shopInfoSerivce.ShopOne(sid);
		if(shopinfo == null ) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS,shopinfo);
	}
}
